<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="userProfile.photo" @click="changeImgFn" />
          <input type="file" ref="iptFile" v-show="false" accept="image/*" @change="onFileChange" />
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="userProfile.name" @click="nameClickFn" />
      <van-cell title="生日" is-link :value="userProfile.birthday" @click="showDateFn" />
    </van-cell-group>

    <van-dialog v-model="show" title="修改名称" show-cancel-button :before-close="updateNameFn">
      <input type="text" v-focus v-model="inputUserName">
    </van-dialog>
    <van-popup v-model="dateShow" position="bottom" :style="{ height: '50%' }" round>
      <van-datetime-picker v-model="currentDate" type="date" title="请选择你的出生日期" :min-date="minDate"
        :max-date="maxDate" @cancel="pickerCancelFn" @confirm="pickerConfirmFn" />
    </van-popup>
  </div>
</template>

<script>
import { getUserProfileAPI, updateUserPhotoAPI, updateUserProfileAPI } from '@/api'
import { Notify } from 'vant'
import { dateFormat } from '@/utils/date'

export default {
  name: 'MyUserEdit',
  data () {
    return {
      userProfile: {
        name: '',
        gender: 0,
        birthday: '',
        intro: ''
      },
      show: false,
      dateShow: false,
      inputUserName: '',
      minDate: new Date(1922, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(2021, 0, 17)
    }
  },
  components: {
  },
  methods: {
    // 1.文件选择事件改变
    async onFileChange (e) {
      if (e.target.files.length === 0) {
        return
      }
      const formData = new FormData()
      formData.append('photo', e.target.files[0])
      const { data: res } = await updateUserPhotoAPI(formData)
      this.userProfile.photo = res.data.photo
      this.$store.commit('SET_USER_PHOTO', this.userProfile.photo)
    },

    // 2.图片点击事件
    changeImgFn () {
      this.$refs.iptFile.click()
    },

    // 3.修改姓名
    nameClickFn () {
      this.show = true
      this.inputUserName = this.userProfile.name
    },

    // 4.确认修改姓名
    async updateNameFn (action, done) {
      if (action === 'confirm') {
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.inputUserName)) {
          await updateUserProfileAPI({ name: this.inputUserName })
          this.userProfile.name = this.inputUserName
          Notify({ type: 'success', message: '修改姓名成功' })
          done()
        } else {
          Notify({ type: 'warning', message: '用户名只能是1-7位中英文字符' })
          done(false)
        }
      } else {
        done()
      }
    },

    // 5.点击生日修改事件
    showDateFn () {
      this.dateShow = true
      this.currentDate = new Date(this.userProfile.birthday)
    },

    // 6.点击日期选择器取消
    pickerCancelFn () {
      this.dateShow = false
    },

    // 7.点击日期选择器确认
    async pickerConfirmFn () {
      await updateUserProfileAPI({ birthday: dateFormat(this.currentDate) })
      this.userProfile.birthday = dateFormat(this.currentDate)
      this.dateShow = false
    }
  },

  async created () {
    const { data: res } = await getUserProfileAPI()
    this.userProfile = res.data
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;

  .avatar {
    width: 50px;
    height: 50px;
  }
}

/deep/ .van-icon-arrow-left {
  color: #fff5ff;
}

.van-dialog {
  text-align: center;

  input {
    text-align: center;
    outline: none;
    border: none;
    line-height: 30px;
    font-size: 14px;
  }
}
</style>
